/**
 *  Copyright (C) 2013  Piotr Szczepa�ski
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package edu.pw.elka.gtsna.arnetminer;

import edu.pw.elka.gtsna.graph.Edge;
import edu.pw.elka.gtsna.graph_creator.GraphFileReader;

/**
 * @author Piotr Lech Szczepa�ski
 * @author P.Szczepanski@ii.pw.edu.pl 
 *
 */
public class ArnetMinerReader extends GraphFileReader {
	
	public ArnetMinerReader(String fileName) {
		super(fileName);
		readLine(); 		
		// first line indicates the number of nodes 
		setNumOfNodes(Integer.valueOf(newLine));			
		nextArticleWithReferences(); 	
	}


	private boolean isLineIndex() {
		return (newLine.length() > 6 && newLine.substring(0, 6).equals("#index"));
	}
	
	private boolean isLineCitation() {
		return (newLine.length() > 3 && newLine.substring(0, 2).equals("#%"));
	}
	
	private void nextArticleWithReferences(){

		while (hasNext() && !isLineCitation()){
			while (hasNext() && !isLineIndex()){
				readLine(); 		
			}				
			// it is not the end of file
			if (hasNext()){
				edgeFrom = Integer.valueOf(newLine.substring(6))+1; // start indexing from 1
				readLine(); 
			}
	
			while (hasNext() && !isLineIndex() && !isLineCitation() ){
				readLine();
			}				
		}
	}
	
	@Override
	public Edge next() {
		edgeTo = Integer.valueOf(newLine.substring(2))+1; // we start indexing from 1
		Edge e = new Edge(edgeFrom,edgeTo);
		
		readLine();
		
		if (hasNext() && !isLineCitation()){
			nextArticleWithReferences();		
		}
		return e;
	}


}
